package com.example.travel.controller;

import com.example.travel.entity.Admin;
import com.example.travel.entity.User;
import com.example.travel.service.ILoginRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/LoginRegisterController")
public class LoginRegisterController {

    @Autowired
    private ILoginRegisterService loginRegisterService;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @GetMapping("/login")
    public User login(User user) {
        User login = loginRegisterService.login(user);
        if(login != null){
            login.setPassword(null);
        }
        return login;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public int register(User user){
        return loginRegisterService.register(user);
    }

    /**
     * 管理员登录
     * @param admin
     * @return
     */
    @GetMapping("/admin_login")
    public Admin admin_login(Admin admin) {
        Admin admin_login = loginRegisterService.admin_login(admin);
        if(admin_login != null){
            admin_login.setPassword(null);
        }
        return admin_login;
    }
}
